package edu.cuit.canary.controller;

import edu.cuit.canary.dto.OrderDto;
import edu.cuit.canary.po.Order;
import edu.cuit.canary.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Resource private OrderService orderService;

    @PostMapping
    public Order createOrder(@RequestBody Order order) {
        return orderService.insertOrder(order);
    }

    @GetMapping("/{id}")
    public OrderDto findOrderById(@PathVariable Long id) { //@RequestHeader String origin
        var sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        //获取本次请求的请求对象
        var request = sra.getRequest();
        System.out.println("origin:" + request.getHeader("origin"));
        return orderService.findOrderById(id);
    }
}
